import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
import {searchH5Sys} from '@/common/vmeitime-http/index.js'
import {isEmpty} from '@/common/graceChecker.js'

const store = new Vuex.Store({
    state: {
        /**
         * 是否需要强制登录
         */
        forcedLogin: false,
        hasLogin: true,	// 是否登录
		userId:'',
		userInfo: {},
		sysInfo:'',	// 系统配置信息
    },
    mutations: {
        loginx(state, userInfo) {
            state.userId = userInfo;
            state.hasLogin = true;
			state.userInfo = userInfo;
			uni.setStorage({	// 将数据存储在本地缓存
			    key: 'userInfo',
			    data: userInfo,
			})
        },
        logout(state) {
            state.userId = "";
			state.userInfo = "";
            state.hasLogin = false;
			uni.removeStorage({		// 从本地缓存中异步移除
			    key: 'userInfo',
			})
        },
		sysInfo(state, sysInfo){
			state.sysInfo = sysInfo
		}
    },
	actions: {
		// 获取配置信息
		getSysInfo({ commit, state }) {
			return new Promise((resolve, reject) => {
			  if (!isEmpty(state.sysInfo)) {
				resolve(state.sysInfo);
			  } else {
				  searchH5Sys().then(res=>{
					  resolve(res);
				  }).catch(error => {
					reject(error);
				  });
			  }
			})
		}
	}
})

export default store
